{% import 'Dockerfile/docker.jinja2' as docker %}
{% import 'Dockerfile/provision.jinja2' as provision %}

{% macro general(role='') -%}
ENV RAILS_ENV="production"

ENV SQLITE_CLEANUP_DAYS=0

# NGINX reverse proxy
RUN export DEBIAN_FRONTEND=noninteractive && set -x \
       && echo deb https://apt.dockerproject.org/repo debian-jessie main > /etc/apt/sources.list.d/docker.list \
       && curl -fsSL https://yum.dockerproject.org/gpg | apt-key add - \
    && apt-install \
        # Install nginx
        nginx \
        # Install docker
        docker-engine \
        # Install php
        php-cli \
        php-mysqlnd \
        php-mcrypt \
        php-curl \
        php-recode \
        php-json \
        # Install java
        openjdk-8-jre \
        # SQlite 3
        sqlite3 \
    && pip install -U \
        docker-compose \
        python-dotenv \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer \
    && curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin/ --filename=composer1 --version=1.10.16 \
    ## Enable ansible for deployment user
    && chmod 755 /usr/local/bin/ansible* \
    {{ docker.cleanup() }}

# NPM stack
RUN curl -sL https://deb.nodesource.com/setup_9.x | bash - \
    && apt-get install -y nodejs \
    && npm install -g gulp \
    && npm install -g grunt-cli \
    && npm install -g bower \
    && npm install -g npm-cache \
    {{ docker.cleanup() }}

# Deployer stack
RUN set -x \
    # Compiler stuff
    && apt-install \
        build-essential \
    # Deployer: Ansistratno (https://github.com/ansistrano)
    && ansible-galaxy install --force \
        ansistrano.deploy \
        ansistrano.rollback \
    # Deployer: PHP Deployer (http://deployer.org/)
    && wget --quiet -O/usr/local/bin/dep http://deployer.org/deployer.phar \
    && chmod +x /usr/local/bin/dep \
    # Deployer: capistrano (http://capistranorb.com/)
    && gem install capistrano \
    {{ docker.cleanup() }}

# Upload
ADD database.yml    /app/config/database.yml
ADD web/            /app/public/assets/

ADD crontab         /etc/cron.d/webdevops-samson-deployment

RUN rake assets:precompile \
    && docker-service enable cron \
    {{ provision.runRoleInline('samson-deployment', role) }}


{%- endmacro %}
